#fin = open('input', 'r')
#input = fin.readline

class Field:
    def __init__(self, arr):
        self.arr = arr
        self.n = len(arr)
        self.m = len(arr[0])
        self.cc = 0
        for x in self.arr:
            for y in x:
                if y:
                    self.cc += 1

    def fall(self):
        up = False
        for i in reversed(range(self.n)):
            for j in range(self.m):
                while (self.arr[i][j]) and (i < self.n-1) and (not self.arr[i+1][j]):
                    self.arr[i+1][j] = self.arr[i][j]
                    self.arr[i][j] = 0
                    up = True
        return up

    def erase(self):
        up = False
        dir = [[0,1],[1,0],[1,1],[1,-1]]
        for x in range(self.n):
            for y in range(self.m):
                if self.arr[x][y]:
                    for dx,dy in dir:
                        x0,y0 = x+dx,y+dy
                        c = 1
                        while  (x0 >= 0) and (y0 >= 0) and (x0 < n) and (y0 < m) and (abs(self.arr[x0][y0]) == abs(self.arr[x][y])):
                            x0+=dx
                            y0+=dy
                            c+=1
                        if c >= 3:
                            x0,y0 = x,y
                            while (x0 >= 0) and (y0 >= 0) and (x0 < n) and (y0 < m) and (abs(self.arr[x0][y0]) == abs(self.arr[x][y])):
                                self.arr[x0][y0] = -abs(self.arr[x0][y0])
                                x0+=dx
                                y0+=dy       
                                up = True   
        for i in range(self.n):
            for j in range(self.m):
                if self.arr[i][j] < 0:
                    self.arr[i][j] = 0
        return up

    def update(self):
        while self.fall() or self.erase():
            pass
            
    def put(self, shr, shu, fig):
        fig = (fig+fig)[shu:shu+3]
        for x in range(3):
            self.arr[x][shr] = fig[x]
            
    def calc(self, shr, shu, fig):
        self.arrs = [[y for y in x] for x in self.arr]
        self.put(shr,shu,fig)
        #print(self.arr)
        self.update()
        cd = 0
        for x in self.arr:
            for y in x:
                if y:
                    cd += 1
        cd = self.cc - cd + 3
        #print(shr,shu,cd)
        #print(self.arr)
        self.arr = self.arrs
        return cd
         
n,m = list(map(int, input().split()))
arr = [list(map(int, input().split())) for x in range(n)]
fig = [int(input()), int(input()), int(input())]
f = Field(arr)
ans = (0,0,-1)
for shr in range(m):
    for shu in range(3):
        cc = f.calc(shr,shu,fig)
        if cc > ans[2]:
            ans = (shr, shu, cc)
print(ans[0],ans[1],ans[2])
